package com.service;

import java.util.ArrayList;
import java.util.List;
import com.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
import com.mapper.StudentMapper;
@Service
public class StudentServiceImpl implements StudentService
{
        
        @Autowired
	private StudentMapper studentMapper;


	public List<Student> queryStudentList(Student student) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if(student!=null){
			 map.put("uname", student.getUname());
			 map.put("upwd", student.getUpwd());
			 map.put("sno", student.getSno());
			 map.put("jgid", student.getJgid());
			 map.put("name", student.getName());
			 map.put("banji", student.getBanji());
			 map.put("quchong", student.getQuchong());
			 map.put("cid", student.getCid());
		}
		
		List<Student> getStudent = studentMapper.query(map);
		return getStudent;
	}

	public int insertStudent(Student student) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if(student!=null){
			map.put("sno", student.getSno());
			map.put("idnumber", student.getIdnumber());
		}
		List<Student> student2 = studentMapper.queryStudentBySnoOrIdnumber(map);
		if(student2.size()>0){
			return 0;
		}else{
			return studentMapper.insertStudent(student);
		}
	}

	public int deleteStudent(int id) throws Exception {
		return studentMapper.deleteStudent(id);
	}

	public int updateStudent(Student student) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if(student!=null){
			map.put("sno", student.getSno());
			map.put("idnumber", student.getIdnumber());
		}
		Student data = studentMapper.queryStudentById(student.getId().intValue());
		if(!data.getIdnumber().equals(student.getIdnumber())||!data.getSno().equals(student.getSno())){
			List<Student> student2 = new ArrayList<Student>();
			student2 = studentMapper.queryStudentBySnoOrIdnumber(map);
			int number=student2.size();
			if(number>0){
				for(Student TempFilep:student2){
					if(TempFilep.getId()==data.getId()){
						number--;
					}
				}
				if(number>0){
					return 0;
				}
				else{
					return studentMapper.updateStudent(student);
				}
			}else{
				return studentMapper.updateStudent(student);
			}
		}else{
			
		}
		
		return studentMapper.updateStudent(student);
		
	}
	
	public Student queryStudentById(int id) throws Exception {
		return studentMapper.queryStudentById(id);
	}
 
}
